﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#endregion

namespace TheSpirit
{
    public abstract class Map
    {
        #region Declaration

        protected Texture2D backgroundTexture;
        protected Rectangle sourceRectangle;
        protected Point worldLocation;
        protected Point localLocation;

        protected List<Enemy> closeEnemy = new List<Enemy>();
        protected List<Enemy> openEnemy = new List<Enemy>();

        double startTime;
        bool isStart = false;
        double interval;
        bool isGenerate = false;

        #endregion


        #region Properties

        public List<Enemy> CloseEnemy
        {
            get { return closeEnemy; }
        }
        public List<Enemy> OpenEnemy
        {
            get { return openEnemy; }
        }
        public Texture2D BackgroundTexture
        {
            get { return backgroundTexture; }
        }
        public Point WorldLocation
        {
            set { worldLocation = value; }
            get { return worldLocation; }
        }
        public Point LocalLocation
        {
            set { localLocation = value; }
            get { return localLocation; }
        }

        #endregion


        #region Abstract Method

        public abstract void LoadContent(ContentManager content);
        public abstract void Draw(SpriteBatch spriteBatch);
        public abstract void SetTile(Tile[,] tile, ContentManager content);
        public abstract void SetEnemy(Tile[,] tile, ContentManager content);

        #endregion


        #region Method

        public void Update(GameTime gameTime, int tileHeight, int tileWidth, Tile[,] tile)
        {
            //화면 이동
            MoveScreen();

            if (!isStart)
            {
                startTime = gameTime.TotalGameTime.TotalSeconds;
                isStart = true;

                foreach (Enemy e in closeEnemy)
                    e.Explore(tile, tileHeight, tileWidth);
            }

            if ((startTime + 5.0f) < gameTime.TotalGameTime.TotalSeconds)
            {
                if (!isGenerate)
                {
                    interval = gameTime.TotalGameTime.TotalSeconds;
                    isGenerate = true;
                }

                if (interval + 2.0f < gameTime.TotalGameTime.TotalSeconds)
                {
                    if (closeEnemy.Count > 0)
                    {
                        openEnemy.Add(closeEnemy[0]);
                        closeEnemy.RemoveAt(0);
                    }
                    isGenerate = false;
                }
            }

            foreach (Enemy e in openEnemy)
            {
                e.Update(gameTime, worldLocation.X, worldLocation.Y, tile);
            }
            //죽거나 도착한 적 제거
            for (int i = 1; i < openEnemy.Count + 1; i++)
            {
                if (!openEnemy[openEnemy.Count - i].IsAlive ||
                    openEnemy[openEnemy.Count - i].IsGoal)
                {
                    openEnemy.RemoveAt(openEnemy.Count - i);
                }
            }
        }

        public void MoveScreen()
        {
            //마우스로 맵 이동
            if (InputManager.RightEnd)
                worldLocation.X+=3;
            if (InputManager.LeftEnd)
                worldLocation.X-=3;
            if (InputManager.UpEnd)
                worldLocation.Y-=3;
            if (InputManager.DownEnd)
                worldLocation.Y+=3;
            //키보드로 맵 이동
            if (InputManager.IsKeyPressed(Keys.Right))
                worldLocation.X += 3;
            if(InputManager.IsKeyPressed(Keys.Left))
                worldLocation.X -= 3;
            if(InputManager.IsKeyPressed(Keys.Up))
                worldLocation.Y -= 3;
            if(InputManager.IsKeyPressed(Keys.Down))
                worldLocation.Y += 3;

            //Boundary
            if (worldLocation.X <= 0)
                worldLocation.X = 0;
            if (worldLocation.X + 1000 >= backgroundTexture.Width)
                worldLocation.X = backgroundTexture.Width - 1000;
            if (worldLocation.Y <= 0)
                worldLocation.Y = 0;
            if (worldLocation.Y + 660 >= backgroundTexture.Height)
                worldLocation.Y = backgroundTexture.Height - 660;
        }

        #endregion
    }
}
